import auth from "@/api/auth";
import { defineStore } from "pinia";

export const usePermissionStore = defineStore("permission", {
  state: () => ({
    routes: [],
    asyncRoutes: [],
  }),
  actions: {
    setRoutes(routes) {
      this.routes = routes;
    },
    setAsyncRoutes(routes) {
      // console.log(routes);
      this.asyncRoutes = routes;
    },
    async fetchAsyncRoutes() {
      const resp = await auth.getUserMenus();
      let userMenus = resp.data || [];
      userMenus = userMenus.filter(
        (menu) => menu.menuType === "menu" && menu.status === "1"
      );
      // console.log(userMenus);
      const asyncRoutes = userMenus.map((menu) => {
        return {
          path: menu.path,
          name: menu.routeName,
          component: menu.component,
          meta: {
            title: menu.menuName,
            icon: menu.icon,
          },
        };
      });
      this.setAsyncRoutes(asyncRoutes);
    },
    async clearAsyncRoutes() {
      this.setAsyncRoutes([]);
    },
  },
});
